import React, { useState } from 'react';
import { Form, Input, InputNumber, message } from 'antd';
import an58 from '@mmstudio/an000058';
import EditButton from '../../../components/editbutton';
import { Message as M1, Result as R1 } from '../../api/admin/dictsys/edit.api';
import api from '../../../atoms/api';

export default function Edit({ data }: {
	data: M1;
}) {
	const [d, setd] = useState(data);
	return <>
		<EditButton
			dlgtitle='值列维护'
			onShow={() => {
				setd(data);
			}}
			onSave={async () => {
				if (!d) {
					return '信息输入有误!';
				}
				//不能为空
				if (!d.type) {
					return '类型不能为空，请填写完整';
				}
				if (!d.name) {
					return '名称不能为空，请填写完整';
				}
				if (!d.key) {
					return '键不能为空，请填写完整';
				}
				if (!d.value) {
					return '值不能为空，请填写完整';
				}
				//修改
				const res = await an58<R1, M1>(api['/api/admin/dictsys/edit'], 'post', d);
				if (res.ok === true) {
					return true;
				}
				return res.message;
			}}>
			<Form.Item required label="类型:" labelCol={{ span: 5 }}>
				<Input placeholder='请输入类型' maxLength={25} value={d.type} onChange={(e) => {
					setd({
						...d,
						type: e.target.value.trim()
					});
				}} />
			</Form.Item>
			<Form.Item required label="名称:" labelCol={{ span: 5 }}>
				<Input placeholder='请输入名称' maxLength={25} value={d.name} onChange={(e) => {
					setd({
						...d,
						name: e.target.value.trim()
					});
				}} />
			</Form.Item>
			<Form.Item required label="键:" labelCol={{ span: 5 }}>
				<InputNumber placeholder='请输入键' value={d.key} onChange={(e) => {
					setd({
						...d,
						key: e
					});
				}} />
			</Form.Item>
			<Form.Item required label="值:" labelCol={{ span: 5 }}>
				<Input placeholder='请输入值' maxLength={25} value={d.value} onChange={(e) => {
					setd({
						...d,
						value: e.target.value.trim()
					});
				}} />
			</Form.Item>
		</EditButton>
		<style jsx>{`
		.ta{
width:100%;
height:10rem;
}
.da{
width:100%;
height:10rem;
}
		`}</style>
	</>;
}
